﻿using Kang.Monitor.Wpf.Base;
using Kang.Monitor.Wpf.Service;
using Kang.Monitor.Wpf.Views;
using MaterialDesignThemes.Wpf;
using System.Windows;

namespace Kang.Monitor.Wpf.ViewModels
{
    internal class LoginViewModel : BaseNotify
    {
        private string _account;

        public string Account
        {
            get { return _account; }
            set { SetProperty(ref _account, value); }
        }

        private string _pwd;

        public string Pwd
        {
            get { return _pwd; }
            set { SetProperty(ref _pwd, value); }
        }

        private string _message;

        public string Message
        {
            get { return _message; }
            set { SetProperty(ref _message, value); }
        }

        //登录命令
        public Command LoginCommand { get; private set; }

        public LoginViewModel(ILoginService loginService)
        {
            LoginCommand = new Command(Login);
            this._loginService = loginService;
        }

        private readonly ILoginService _loginService;

        private async void Login(object obj)
        {
            if (string.IsNullOrEmpty(Account))
            {
                Message = "账号错误！";
                return;
            }
            if (string.IsNullOrEmpty(Pwd))
            {
                Message = "密码错误！";
                return;
            }
            var result = _loginService.Login(Account, Pwd).Result;
            if (result.Status)
            {
                //跳转到首页
                (obj as LoginView).DialogResult = true;
                //Application.Current.Dispatcher?.Invoke(() =>
                //{
                //});
            }
            else
            {
                Message = "登录失败,请重新登录！";
            }
        }
    }
}